#!/usr//bin/python

import serial

#Finds how many serial ports are available

available = []
for i in range(256):
	try:
		s = serial.Serial(i)
		available.append( (i, s.portstr))
		s.close()   # explicit close 'cause of delayed GC in java
	except serial.SerialException:
		pass
print "Available serial ports:"
for n,s in available:
	print "\t(%d) %s" % (n,s)

#Now we can generate the config file, which will be edited from the frontend
#In fact there is almost nothing in this file : only the available ports...
f = open("./config", 'w')
for n,s in available:
	f.write("%d=%s\n" % (n,s))
f.close()

print "Done. The list of available serial ports is stored in config"

print "***** to do before you start *****"
print "\tSet your environnment variables so that you can kernel compilation doesn't fail."
print "\tCopy and paste in your root fs the files ./initScripts/rcS and ./initScripts/S80initCallScheduler. (They should be pasted in pathtoyourfs/build_mipsel/root/etc/init.d)"

print "\tIf you are using a USB to serial cable please change the corresponding line in ./config."
print "\tEx: 0=/dev/ttyS0 becomes 0=/dev/ttyUSB0"

print "\n Once you're done you can run the start script : ./start.py."
